package cn.com.open.itembank.logic.questiontype;

import cn.com.open.itembank.common.Function;
import cn.com.open.itembank.dto.QuestionImportModel;
import cn.com.open.itembank.dto.QuestionModel;

/**
 * Created by lucky on 2017/12/19.
 */
public class OrderItem extends QuestionTypeBase {
    /**
     * Instantiates a new Order item.
     *
     * @param model the model
     */
    public OrderItem(QuestionImportModel model) {
        super(model);
        super.mustHasChoice = true;
    }

    //region 校验答案格式

    /**
     * 校验答案格式.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkAnswerFormat() {
        boolean result = true;

        for (String item : importModel.getAnswer()) {
            if (answerIdList.indexOf(item.trim().toUpperCase()) < 0) {
                importModel.setErrorInfo("答案格式不正确；");
                result = false;
                break;
            }
        }

        return result;
    }
    //endregion

    //region 校验选项与答案数量是否匹配

    /**
     * 校验选项与答案数量是否匹配.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkMatch() {
        boolean result = true;

        if (importModel.getChoices().size() != importModel.getAnswer().size()) {
            importModel.setErrorInfo("选项与答案数量不匹配；");
            result = false;
        }

        return result;
    }
    //endregion

    //region 获取保存模型

    /**
     * 获取保存模型.
     *
     * @return the input question model
     */
    @Override
    public QuestionModel getSaveModel() {
        QuestionModel model = super.getSaveModel();

        //I6,I7 需要各种题型重载完成
        for (String choice : importModel.getChoices()) {
            model.getI6().add(choice);
        }
        //answer存的是abcd，i7需要0123
        for (String answer : importModel.getAnswer()) {
            model.getI7().add(String.valueOf(Function.getUpperCaseIndex(answer)));
        }

        return model;
    }
    //endregion
}
